package com.ygqh.baby.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ygqh.baby.annotation.WebLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Constant;
import com.ygqh.baby.ao.CustomerStatus;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.CustomerUser;
import com.ygqh.baby.model.QuickReply;
import com.ygqh.baby.model.YgUserModel;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.ChatRecordService;
import com.ygqh.baby.service.CustomerUserService;
import com.ygqh.baby.service.QuickReplyService;
import com.ygqh.baby.service.YgUserService;

/**
 * @author Ss
 */
@Controller
@RequestMapping("/chat/adminuser")
public class AdminUserController extends BaseController {

	@Autowired
	private CustomerUserService customerUserService;
	@Autowired
	private QuickReplyService quickReplyService;
	@Autowired
	private YgUserService ygUserService;
	@Autowired
	private ChatRecordService chatRecordService;

	/**
	 * 保存客服信息
	 *
	 * @param customerUser
	 * @param optionName
	 * @param callback
	 * @return
	 */
	@WebLog("保存客服信息")
	@RequestMapping("/saveCustomerUser")
	@ResponseBody
	public JSONPObject saveCustomerUser(CustomerUser customerUser, String optionName, String callback) {
		CustomerUser tempCustomer = customerUserService.isExistsByUserName(customerUser.getUserName(), customerUser.getId());
		if (tempCustomer != null) {
			return new JSONPObject(callback, Message.error(Constant.YG_USER_IS_REPEAT));
		}
		if (StringUtils.isNotBlank(customerUser.getId())) {
			CustomerUser dbCustomer = customerUserService.findUserById(customerUser.getId());
			dbCustomer.setReceptionRatio(customerUser.getReceptionRatio());
			dbCustomer.setTrueName(customerUser.getTrueName());
			dbCustomer.setNickName(customerUser.getNickName());
			dbCustomer.setIsDefault(customerUser.getIsDefault());
			dbCustomer.setUpdateTime(new Date());
			dbCustomer.setUpdateBy(optionName);
			customerUserService.updateEntity(dbCustomer);
			return new JSONPObject(callback, Message.success(dbCustomer));
		}
		YgUser ygUser = ygUserService.findByUserName(customerUser.getUserName());
		if (ygUser == null) {
			return new JSONPObject(callback, Message.error(Constant.YG_USER_IS_NULL));
		}
		customerUser.setId(null);
		customerUser.setUserName(ygUser.getUserName());
		customerUser.setHeadFileId(ygUserService.saveHeadImageToMongo(ygUser.getHeadImageUrl(), customerUser.getUserName()));
		customerUser.setCreateTime(new Date());
		customerUser.setCreateBy(optionName);
		customerUserService.save(customerUser);
		return new JSONPObject(callback, Message.success(customerUser));
	}

	/**
	 * 修改客服信息数据状态
	 *
	 * @param customerId
	 * @param status
	 * @param callback
	 * @return
	 */
	@WebLog("修改客服信息数据状态")
	@RequestMapping("/updateCustomerUserByStatus")
	@ResponseBody
	public JSONPObject updateCustomerUserByStatus(String customerId, DataStatus status, String callback) {
		return new JSONPObject(callback, customerUserService.updateCustomerUserByStatus(customerId, status));
	}

	/**
	 * 获取客服列表
	 *
	 * @param queryInfo
	 * @param q
	 * @param customerStatus
	 * @param status
	 * @param callback
	 * @return
	 */
	@RequestMapping("/searchCustomerUser")
	@ResponseBody
	public JSONPObject searchCustomerUser(QueryInfo queryInfo, String q, CustomerStatus customerStatus,
										  DataStatus status, String callback) {
		return new JSONPObject(callback,
				Message.success(customerUserService.findCustomerList(queryInfo, q, customerStatus, status)));
	}

	/**
	 * 获取快捷回复列表
	 *
	 * @param queryInfo
	 * @param q
	 * @param status
	 * @param callback
	 * @return
	 */
	@RequestMapping("/searchQuickReply")
	@ResponseBody
	public JSONPObject searchQuickReply(QueryInfo queryInfo, String q,
										DataStatus status, String callback) {
		return new JSONPObject(callback,
				Message.success(quickReplyService.findQuickReplyList(queryInfo, q, status)));
	}

	/**
	 * 保存快捷回复
	 *
	 * @param quickReply
	 * @param optionName
	 * @param callback
	 * @return
	 */
	@WebLog("保存快捷回复")
	@RequestMapping("/saveQuickReply")
	@ResponseBody
	public JSONPObject saveQuickReply(QuickReply quickReply, String optionName, String callback) {
		if (StringUtils.isNotBlank(quickReply.getId())) {
			QuickReply dbQuickReply = quickReplyService.findById(quickReply.getId());
			dbQuickReply.setDetailContent(quickReply.getDetailContent());
			dbQuickReply.setMessageType(quickReply.getMessageType());
			dbQuickReply.setTitle(quickReply.getTitle());
			dbQuickReply.setIsFirst(quickReply.getIsFirst());
			dbQuickReply.setUpdateTime(new Date());
			quickReplyService.update(dbQuickReply);
			return new JSONPObject(callback, Message.success(dbQuickReply));
		}
		quickReply.setId(null);
		quickReply.setCreateTime(new Date());
		quickReplyService.save(quickReply);
		return new JSONPObject(callback, Message.success(quickReply));
	}

	/**
	 * 修改快捷回复状态
	 *
	 * @param id
	 * @param isFirst
	 * @param callback
	 * @return
	 */
	@WebLog("修改快捷回复状态")
	@RequestMapping("/updateQuickReply")
	@ResponseBody
	public JSONPObject updateQuickReplyByIsFirst(String id, Boolean isFirst, DataStatus status, String callback) {
		return new JSONPObject(callback, quickReplyService.updateQuickIsFirst(id, isFirst, status));
	}

	/**
	 * 获取用户列表
	 *
	 * @param q
	 * @param callback
	 * @return
	 */
	@RequestMapping("/findUserListByAdmin")
	@ResponseBody
	public JSONPObject findUserList(String q, String callback) {
		List<YgUserModel> userList = null;
		if (StringUtils.isBlank(q)) {
			userList = customerUserService.findUserList(null);
		} else {
			userList = customerUserService.searchUserList(q);
		}
		return new JSONPObject(callback, Message.success(userList));
	}

	/**
	 * 获取所有用户列表
	 * 不受限于当前客服
	 *
	 * @param q
	 * @param callback
	 * @return
	 */
	@RequestMapping("/searchUserList")
	@ResponseBody
	public JSONPObject searchUserList(@RequestParam(required = true) String q, String callback) {
		return new JSONPObject(callback, Message.success(customerUserService.searchUserList(q)));
	}

	/**
	 * 获取指定用户历史聊天记录
	 * 以30天分页
	 *
	 * @param endTime
	 * @param userId
	 * @param callback
	 * @return
	 */
	@RequestMapping("/findRecordListByAdmin")
	@ResponseBody
	public JSONPObject findRecordList(QueryInfo queryInfo, Long userId, String q, String callback) {
		return new JSONPObject(callback,
				Message.success(chatRecordService.findUserRecordList(Arrays.asList(userId), q, queryInfo)));
	}

	/**
	 * 按日期统计所有接待用户
	 *
	 * @param queryInfo
	 * @param callback
	 * @return
	 */
	@RequestMapping("/findUserReportByAll")
	@ResponseBody
	public JSONPObject findUserReportByAll(QueryInfo queryInfo, String callback) {
		ResultSet<Map<String, Object>> result = chatRecordService.report(queryInfo);
		return new JSONPObject(callback, Message.success(result.toDatatables(null)));
	}

	/**
	 * 按客服统计指定日期接待用户
	 *
	 * @param date
	 * @param callback
	 * @return
	 */
	@RequestMapping("/findUserReportByCustomer")
	@ResponseBody
	public JSONPObject findUserReportByCustomer(Date date, String callback) {
		return new JSONPObject(callback, Message.success(chatRecordService.reportByCustomer(date)));
	}

}
